<template>
  <div>
    <el-dialog class="positionDialog" :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="80%"
      :before-close="handleClose" :modal="false" :close-on-click-modal="false">
      <div class="content">
        <el-divider>通风管理</el-divider>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <div class="form-item">
            <div class="title fir-title">
              <svg-icon class="svg-icon" icon-class="infoAdd" />
              <span>通风填报</span>
            </div>
            <div class="form">
              <el-form-item label="农场名称" prop="farmCode">
                <el-select style="width: 100%;" v-model="ruleForm.farmCode" @change="changeFram" filterable
                  placeholder="请选择农场名称或输入关键词">
                  <el-option v-for="item in SelectList.farm_type" :key="item.value" :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="生产队名称" prop="teamCode">
                <el-select style="width: 100%;" v-model="ruleForm.teamCode" @change="changeFram2" filterable
                  placeholder="请选择生产队名称或输入关键词">
                  <el-option v-for="item in SelectList.teamList" :key="item.value" :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="仓号" prop="wareNumber">
                <el-select style="width: 100%;" v-model="ruleForm.wareNumber" filterable placeholder="请选择仓号或输入关键词">
                  <el-option v-for="item in SelectList.wareNumber" :key="item.label" :label="item.label"
                    :value="item.label">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="风速(m/s)" prop="velocity">
                <el-input v-model="ruleForm.velocity" placeholder="请输入风速"></el-input>
              </el-form-item>
              <el-form-item label="通风目的" prop="airy">
                <el-select style="width: 100%;" v-model="ruleForm.airy" filterable placeholder="请选择通风目的或输入关键词">
                  <el-option v-for="item in SelectList.airy" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="通风时间" prop="time">
                <el-input v-model="ruleForm.time" placeholder="请输入通风时间"></el-input>小时
              </el-form-item>
              <el-form-item label="送风方式" prop="aerate">
                <el-select style="width: 100%;" v-model="ruleForm.aerate" filterable placeholder="请选择送风方式">
                  <el-option v-for="item in SelectList.aerate_type" :key="item.value" :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="气流" prop="airflow">
                <el-select style="width: 100%;" v-model="ruleForm.airflow" filterable placeholder="请选择气流方向">
                  <el-option v-for="item in SelectList.airflow" :key="item.value" :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
          <div class="form-item">
            <div class="title fir-title">
              <svg-icon class="svg-icon" icon-class="infoAdd" />
              <span>填写人信息</span>
            </div>
            <div class="form">
              <el-form-item label="姓名" prop="createUser">
                <el-input v-model="ruleForm.createUser" placeholder="请输入姓名" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="ruleForm.phone" placeholder="请输入手机号"></el-input>
              </el-form-item>
              <el-form-item label="日期时间" prop="">
                <el-date-picker format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" :disabled="true"
                  v-model="ruleForm.createTime" type="datetime" placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
            </div>
          </div>
        </el-form>
      </div>

      <div class="dialog-footer">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import rules from '@/utils/rules'
import { getUserKey } from '@/utils/auth'
import { formatDate } from '@/utils/methods'
import { addWindList, warehouseGetList, updateWindList, getByIdApi } from '@/api/wind'
import { selectApi } from '@/api/select'
export default {
  props: ['showDialog'],
  data() {
    return {
      ruleForm: {
        "farmCode": this.$farmCode,
        "teamCode": this.$teamCode,
        "wareNumber": "",
        "airy": "",
        "velocity": "",
        "time": 0,
        "aerate": "",
        "airflow": "",
        "phone": "",
        "farmName": "",
        "teamName": ""
      },
      SelectList: {
        aerate_type: [],
        airy: [],
        airflow: [],
        farm_type: [],
        teamList: [],
        wareNumber: []
      },
      rules: rules,
      carList: [],
      timeList: []
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        "farmCode": "",
        "teamCode": "",
        "wareNumber": "",
        "airy": "",
        "velocity": "",
        "time": 0,
        "aerate": "",
        "airflow": "",
        "phone": "",
        "farmName": "",
        "teamName": ""
      }
      this.$refs.ruleForm.resetFields();
    },
    changeFram(e) {
      selectApi({ type: e }).then((res) => {
        this.SelectList.teamList = res.data.dictItemList;
      })
    },
    changeFram2(e) {
      warehouseGetList({
        teamCode: e,
        farmCode: this.ruleForm.farmCode,
        cropType: "",
        cropQuality: ""
      }).then((res) => {
        this.SelectList.wareNumber = res.data;
      })
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            let { code } = await updateWindList(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
            }
          } else {
            let { code } = await addWindList(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '添加成功',
                type: 'success'
              });
            }
          }
          this.$emit('handleRefresh')
          this.handleClose()
        } else {
          return false;
        }
      });
    },
    // 获取填报人时间
    handleGetInfo() {
      this.ruleForm.createUser = getUserKey() || 'nkjtcyf'
      this.ruleForm.createTime = formatDate(new Date())
    },
    // 获取通风目的
    async handleGetairy(id) {
      let { data } = await getByIdApi({ id })
      this.ruleForm = data
    },
    setSelectListAll() {
      const list = ['aerate_type', 'airy', 'airflow', "farm_type"];
      const ProList = list.map((item) => {
        return selectApi({ type: item })
      })
      Promise.all(ProList).then((res) => {
        this.SelectList.aerate_type = res[0].data.dictItemList
        this.SelectList.airy = res[1].data.dictItemList
        this.SelectList.airflow = res[2].data.dictItemList
        if (this.$status != 0) {
          this.SelectList.farm_type = res[3].data.dictItemList.filter(item => item.value == this.$farmCode)
        } else {
          this.SelectList.farm_type = res[3].data.dictItemList
        }
      })

      selectApi({ type: this.$farmCode }).then((res) => {
        if (this.$status != '2') {
          this.SelectList.teamList = res.data.dictItemList
        } else {
          this.SelectList.teamList = res.data.dictItemList.filter(item => item.value == this.$teamCode)
          this.changeFram2(this.$teamCode)
        }
      })

    },
  },
  mounted() {
    this.setSelectListAll();

  }
}
</script>

<style lang='scss' scoped>
.el-form {
  width: 100%;
  display: flex;
  justify-content: space-between;

  .form-item {
    width: 45%;
  }
}

.fir-title {
  margin-top: 0 !important;
}

.el-divider {
  margin-bottom: 40px !important;
}

.el-divider--horizontal {
  height: 3px;
  background-color: #16a085;
}

::v-deep .el-divider__text {
  color: #16a085;
  font-size: 38px;
  font-weight: 700;
}


::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  height: 36px;
  line-height: 36px;
}

::v-deep .el-input {
  flex: 1;
  height: 36px;
}

::v-deep .el-select {
  flex: 1;
  height: 36px;
}

::v-deep .el-input__icon {
  line-height: 36px;
}

::v-deep .el-input__inner {
  height: 36px;
}
</style>
